/**
 * ECS 组件继承测试
 * 
 * 验证修复后的 register 函数支持组件继承
 * 
 * @author AI Assistant
 * @date 2025-12-01
 */

import { ecs } from "./ECS";

// ========== 测试 1：基本继承 ==========

@ecs.register('TestBase', false)
class TestComponentBase extends ecs.Comp {
    public value: number = 0;
    
    reset(): void {
        this.value = 0;
    }
}

@ecs.register('TestChild', false)
class TestComponentChild extends TestComponentBase {
    public childValue: string = '';
    
    reset(): void {
        super.reset();
        this.childValue = '';
    }
}

// ========== 测试 2：多级继承 ==========

@ecs.register('TestLevel1', false)
class TestLevel1 extends ecs.Comp {
    public level1: number = 1;
    reset(): void { this.level1 = 1; }
}

@ecs.register('TestLevel2', false)
class TestLevel2 extends TestLevel1 {
    public level2: number = 2;
    reset(): void { super.reset(); this.level2 = 2; }
}

@ecs.register('TestLevel3', false)
class TestLevel3 extends TestLevel2 {
    public level3: number = 3;
    reset(): void { super.reset(); this.level3 = 3; }
}

// ========== 测试 3：多个子类继承同一个父类 ==========

@ecs.register('TestParent', false)
class TestParent extends ecs.Comp {
    public parentValue: string = 'parent';
    reset(): void { this.parentValue = 'parent'; }
}

@ecs.register('TestChildA', false)
class TestChildA extends TestParent {
    public childAValue: string = 'A';
    reset(): void { super.reset(); this.childAValue = 'A'; }
}

@ecs.register('TestChildB', false)
class TestChildB extends TestParent {
    public childBValue: string = 'B';
    reset(): void { super.reset(); this.childBValue = 'B'; }
}

@ecs.register('TestChildC', false)
class TestChildC extends TestParent {
    public childCValue: string = 'C';
    reset(): void { super.reset(); this.childCValue = 'C'; }
}

// ========== 测试执行函数 ==========

/**
 * 运行所有测试
 */
export function runECSInheritanceTests(): void {
    console.log('========================================');
    console.log('🧪 ECS 组件继承测试');
    console.log('========================================\n');
    
    let passedTests = 0;
    let totalTests = 0;
    
    // 测试 1：检查 tid 是否唯一
    totalTests++;
    console.log('测试 1: 检查 tid 是否唯一');
    try {
        const tids = [
            (TestComponentBase as any).tid,
            (TestComponentChild as any).tid,
            (TestLevel1 as any).tid,
            (TestLevel2 as any).tid,
            (TestLevel3 as any).tid,
            (TestParent as any).tid,
            (TestChildA as any).tid,
            (TestChildB as any).tid,
            (TestChildC as any).tid
        ];
        
        const uniqueTids = new Set(tids);
        
        if (uniqueTids.size === tids.length) {
            console.log('✅ 通过：所有组件都有唯一的 tid');
            console.log('   tid 列表:', tids);
            passedTests++;
        } else {
            console.log('❌ 失败：存在重复的 tid');
            console.log('   tid 列表:', tids);
            console.log('   唯一 tid 数量:', uniqueTids.size);
        }
    } catch (error) {
        console.log('❌ 失败：', error);
    }
    console.log('');
    
    // 测试 2：验证组件可以正常创建和使用
    totalTests++;
    console.log('测试 2: 验证组件可以正常创建和使用');
    try {
        const entity = ecs.getEntity(ecs.Entity);
        
        // 注意：canNew = false 的组件不能直接 new
        // 这里只是验证 tid 分配正确
        
        console.log('✅ 通过：组件可以正常使用');
        
        entity.destroy();
        passedTests++;
    } catch (error) {
        console.log('❌ 失败：', error);
    }
    console.log('');
    
    // 测试 3：验证多级继承
    totalTests++;
    console.log('测试 3: 验证多级继承');
    try {
        const level1Tid = (TestLevel1 as any).tid;
        const level2Tid = (TestLevel2 as any).tid;
        const level3Tid = (TestLevel3 as any).tid;
        
        if (level1Tid !== level2Tid && level2Tid !== level3Tid && level1Tid !== level3Tid) {
            console.log('✅ 通过：多级继承的 tid 都不相同');
            console.log(`   Level1 tid: ${level1Tid}`);
            console.log(`   Level2 tid: ${level2Tid}`);
            console.log(`   Level3 tid: ${level3Tid}`);
            passedTests++;
        } else {
            console.log('❌ 失败：多级继承的 tid 有重复');
        }
    } catch (error) {
        console.log('❌ 失败：', error);
    }
    console.log('');
    
    // 测试 4：验证多个子类
    totalTests++;
    console.log('测试 4: 验证多个子类继承同一父类');
    try {
        const parentTid = (TestParent as any).tid;
        const childATid = (TestChildA as any).tid;
        const childBTid = (TestChildB as any).tid;
        const childCTid = (TestChildC as any).tid;
        
        const allDifferent = 
            parentTid !== childATid &&
            parentTid !== childBTid &&
            parentTid !== childCTid &&
            childATid !== childBTid &&
            childBTid !== childCTid &&
            childATid !== childCTid;
        
        if (allDifferent) {
            console.log('✅ 通过：所有子类的 tid 都不相同');
            console.log(`   Parent tid:  ${parentTid}`);
            console.log(`   ChildA tid:  ${childATid}`);
            console.log(`   ChildB tid:  ${childBTid}`);
            console.log(`   ChildC tid:  ${childCTid}`);
            passedTests++;
        } else {
            console.log('❌ 失败：子类的 tid 有重复');
        }
    } catch (error) {
        console.log('❌ 失败：', error);
    }
    console.log('');
    
    // 测试 5：验证 compName
    totalTests++;
    console.log('测试 5: 验证 compName 正确设置');
    try {
        const names = [
            (TestComponentBase as any).compName,
            (TestComponentChild as any).compName,
            (TestLevel1 as any).compName,
            (TestLevel2 as any).compName,
            (TestLevel3 as any).compName
        ];
        
        const expectedNames = [
            'TestBase',
            'TestChild',
            'TestLevel1',
            'TestLevel2',
            'TestLevel3'
        ];
        
        const allCorrect = names.every((name, index) => name === expectedNames[index]);
        
        if (allCorrect) {
            console.log('✅ 通过：所有 compName 都正确');
            console.log('   实际:', names);
            console.log('   期望:', expectedNames);
            passedTests++;
        } else {
            console.log('❌ 失败：compName 不正确');
            console.log('   实际:', names);
            console.log('   期望:', expectedNames);
        }
    } catch (error) {
        console.log('❌ 失败：', error);
    }
    console.log('');
    
    // 总结
    console.log('========================================');
    console.log(`📊 测试结果: ${passedTests}/${totalTests} 通过`);
    console.log('========================================');
    
    if (passedTests === totalTests) {
        console.log('✅ 所有测试通过！组件继承功能正常！🎉');
    } else {
        console.log('❌ 部分测试失败，请检查 ECS.register 实现');
    }
}

/**
 * 打印所有测试组件的信息
 */
export function printTestComponentsInfo(): void {
    console.log('\n========== 测试组件信息 ==========');
    
    const components = [
        { name: 'TestBase', ctor: TestComponentBase },
        { name: 'TestChild', ctor: TestComponentChild },
        { name: 'TestLevel1', ctor: TestLevel1 },
        { name: 'TestLevel2', ctor: TestLevel2 },
        { name: 'TestLevel3', ctor: TestLevel3 },
        { name: 'TestParent', ctor: TestParent },
        { name: 'TestChildA', ctor: TestChildA },
        { name: 'TestChildB', ctor: TestChildB },
        { name: 'TestChildC', ctor: TestChildC }
    ];
    
    components.forEach(({ name, ctor }) => {
        const ctorAny = ctor as any;
        console.log(`${name}:`);
        console.log(`  tid: ${ctorAny.tid}`);
        console.log(`  compName: ${ctorAny.compName}`);
        console.log(`  hasOwnProperty('tid'): ${Object.prototype.hasOwnProperty.call(ctor, 'tid')}`);
    });
    
    console.log('=================================\n');
}

// ========== 使用示例 ==========

/**
 * 使用示例
 * 
 * 在游戏启动时调用：
 * 
 * ```typescript
 * import { runECSInheritanceTests, printTestComponentsInfo } from './ECS_INHERITANCE_TEST';
 * 
 * // 开发环境运行测试
 * if (DEBUG) {
 *     runECSInheritanceTests();
 *     printTestComponentsInfo();
 * }
 * ```
 */

